﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication.Controls
{
    public partial class ShowCharacterDetails1 : UserControl
    {
        public event EventHandler<ObjectEventArgs<GameChar>> WantEnterGame;
        public event EventHandler WantClose;

        public static readonly DependencyProperty GameCharProperty = DependencyProperty.Register(
            "GameChar", typeof(GameChar), typeof(ShowCharacterDetails1), new PropertyMetadata(null, new PropertyChangedCallback(GameCharChanged)));

        public GameChar GameChar
        {
            get { return (GameChar)GetValue(GameCharProperty); }
            set { SetValue(GameCharProperty, value); }
        }

        private static void GameCharChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ShowCharacterDetails1 t = (ShowCharacterDetails1)d;
            t.GameChar = e.NewValue as GameChar;
            t.UpdateBindings();
            t.Dispatcher.BeginInvoke(() => t.btnEnter.IsEnabled = true);
        }

        public void UpdateBindings()
        {
            DataContext = GameChar as GameChar;            
            editGameThing.EditObj = GameChar;
            editGameThing.FilterHideHeader = new Func<WindowContainer, bool>((wc) => wc.DescriptionTextString.IsIn("Abilities", "States"));
            editGameThing.HideHeader = true;
        }

        public static readonly DependencyProperty ShowEnterProperty = DependencyProperty.Register(
            "ShowEnter", typeof(bool), typeof(ShowCharacterDetails1), new PropertyMetadata(false, new PropertyChangedCallback(ShowEnterTemplateChanged)));

        public bool ShowEnter
        {
            get { return (bool)GetValue(ShowEnterProperty); }
            set { SetValue(ShowEnterProperty, value); }
        }

        private static void ShowEnterTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ShowCharacterDetails1 t = (ShowCharacterDetails1)d;
            t.ShowEnter = (bool)e.NewValue;
            t.UpdateBindingsShowEnter();
        }

        public void UpdateBindingsShowEnter()
        {
            btnEnter.Visibility = (ShowEnter) ? Visibility.Visible : Visibility.Collapsed;
        }

        public ShowCharacterDetails1()
        {
            InitializeComponent();            
            Loaded += new RoutedEventHandler(ShowCharacterDetails1_Loaded);
            SizeChanged += new SizeChangedEventHandler(ShowCharacterDetails1_SizeChanged);
        }

        void ShowCharacterDetails1_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            
        }

        public void SetHeight(double height)
        {
            wc2.Height = height;            
        }

        void ShowCharacterDetails1_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateBindingsShowEnter();
        }

        private void btnEnter_Click(object sender, RoutedEventArgs e)
        {
            btnEnter.IsEnabled = false;
            EnterGame();
        }

        public void EnterGame()
        {
            if (WantEnterGame != null)
                WantEnterGame(this, new ObjectEventArgs<GameChar>(GameChar as GameChar));

            if (WantClose != null)
                WantClose(this, new EventArgs());
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (btnEnter.Visibility == Visibility.Visible)
                    EnterGame();
            }
        }

        private void WindowContainer_CloseWindow(object sender, EventArgs e)
        {
            if (WantClose != null)
                WantClose(this, new EventArgs());
        }
    }
}
